﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CStorableData))]
    public class CStorable: CComponent
    {
        #region identifier

        public delegate void DOnClickEventHandler();

        #endregion

        #region fields

        private CContainer m_containerComponent;

        private CVignette m_vignette;
        private bool m_wasRightClickedOnVignette;
        private bool m_wasMiddleClickedOnVignette;
        private bool m_wasLeftClickedOnVignette;

        public event DOnClickEventHandler onRightClickOnModel;
        public event DOnClickEventHandler onLeftClickOnModel;
        public event DOnClickEventHandler onMiddleClickOnModel;
        public event DOnClickEventHandler onRightReleaseOnModel;
        public event DOnClickEventHandler onLeftReleaseOnModel;
        public event DOnClickEventHandler onMiddleReleaseOnModel;

        public event DOnClickEventHandler onRightClickOnVignette;
        public event DOnClickEventHandler onLeftClickOnVignette;
        public event DOnClickEventHandler onMiddleClickOnVignette;
        public event DOnClickEventHandler onRightReleaseOnVignette;
        public event DOnClickEventHandler onLeftReleaseOnVignette;
        public event DOnClickEventHandler onMiddleReleaseOnVignette;

        #endregion

        #region properties

        [ContentSerializerIgnore]
        public CContainer ContainerComponent { get { return m_containerComponent; } set { m_containerComponent = value; } }

        [ContentSerializerIgnore]
        public CVignette Vignette 
        {
            get { return m_vignette; } 
            set
            {
                if (m_vignette != null)
                {
                    m_vignette.OnClick -= OnClickOnVignette;
                    m_vignette.OnRelease -= OnReleaseOnVignette;
                }
                m_vignette = value;
            }
        }

        [ContentSerializerIgnore]
        public override CGameObject GameObject
        {
            get
            {
                return base.GameObject;
            }
            set
            {
                if (m_gameObject != null)
                {
                    m_gameObject.OnClick -= OnClickOnModel;
                    m_gameObject.OnRelease -= OnReleaseOnModel;
                }
                base.GameObject = value;
            }
        }

        #endregion

        #region methods

        public override CComponent Clone(CGameObject gameObject)
        {
            CStorable stb = new CStorable();
            stb.GameObject = gameObject;
            stb.Vignette = m_vignette.Clone() as CVignette;
            stb.onRightClickOnModel = onRightClickOnModel;
            stb.onLeftClickOnModel = onLeftClickOnModel;
            stb.onMiddleClickOnModel = onMiddleClickOnModel;
            stb.onRightReleaseOnModel = onRightReleaseOnModel;
            stb.onLeftReleaseOnModel = onLeftReleaseOnModel;
            stb.onMiddleReleaseOnModel = onMiddleReleaseOnModel;
            stb.onRightClickOnVignette = onRightClickOnVignette;
            stb.onLeftClickOnVignette = onLeftClickOnVignette;
            stb.onMiddleClickOnVignette = onMiddleClickOnVignette;
            stb.onRightReleaseOnVignette = onRightReleaseOnVignette;
            stb.onLeftReleaseOnVignette = onLeftReleaseOnVignette;
            stb.onMiddleReleaseOnVignette = onMiddleReleaseOnVignette;
            return stb;
        }

        public override void Initialize(CGameObject gameObject)
        {
            m_gameObject.OnClick -= OnClickOnModel;
            m_gameObject.OnRelease -= OnReleaseOnModel;
            m_vignette.OnClick -= OnClickOnVignette;
            m_vignette.OnRelease -= OnReleaseOnVignette; 
            m_gameObject.OnClick += OnClickOnModel;
            m_gameObject.OnRelease += OnReleaseOnModel;
            m_vignette.OnClick += OnClickOnVignette;
            m_vignette.OnRelease += OnReleaseOnVignette;
            m_wasRightClickedOnVignette = false;
            m_wasMiddleClickedOnVignette = false;
            m_wasLeftClickedOnVignette = false;
        }
        
        public void OnClickOnVignette(CWidget sender)
        {
            if (sender.IsRightClicked && !m_wasRightClickedOnVignette && onRightClickOnVignette != null)
            {
                onRightClickOnVignette.Invoke();
                m_wasRightClickedOnVignette = true;
            }
            if (sender.IsLeftClicked && !m_wasLeftClickedOnVignette && onLeftClickOnVignette != null)
            {
                onLeftClickOnVignette.Invoke();
                m_wasLeftClickedOnVignette = true;
            }
            if (sender.IsMiddleClicked && !m_wasMiddleClickedOnVignette && onMiddleClickOnVignette != null)
            {
                onMiddleClickOnVignette.Invoke();
                m_wasMiddleClickedOnVignette = true;
            } 

        }

        public void OnReleaseOnVignette(CWidget sender)
        {
            if (!sender.IsRightClicked && m_wasRightClickedOnVignette && onRightReleaseOnVignette != null)
            {
                onRightReleaseOnVignette.Invoke();
                m_wasRightClickedOnVignette = false;
            }
            if (!sender.IsLeftClicked && m_wasLeftClickedOnVignette && onLeftReleaseOnVignette != null)
            {
                onLeftReleaseOnVignette.Invoke();
                m_wasLeftClickedOnVignette = false;
            }
            if (!sender.IsMiddleClicked && m_wasMiddleClickedOnVignette && onMiddleReleaseOnVignette != null)
            {
                onMiddleReleaseOnVignette.Invoke();
                m_wasMiddleClickedOnVignette = false;
            }
        }

        public void OnClickOnModel(CGameObject gameObject, CMouse.EButton button)
        {
            switch (button)
            {
                case CMouse.EButton.LEFT:
                    if (onLeftClickOnModel != null)
                        onLeftClickOnModel.Invoke();
                    break;
                case CMouse.EButton.RIGHT:
                    if (onRightClickOnModel != null)
                        onRightClickOnModel.Invoke();
                    break;
                case CMouse.EButton.MIDDLE:
                    if (onMiddleClickOnModel != null)
                        onMiddleClickOnModel.Invoke();
                    break;
            }
        }

        public void OnReleaseOnModel(CGameObject gameObject, CMouse.EButton button)
        {
            switch (button)
            {
                case CMouse.EButton.LEFT:
                    if (onLeftReleaseOnModel != null)
                        onLeftReleaseOnModel.Invoke();
                    break;
                case CMouse.EButton.RIGHT:
                    if (onRightReleaseOnModel != null)
                        onRightReleaseOnModel.Invoke();
                    break;
                case CMouse.EButton.MIDDLE:
                    if (onMiddleReleaseOnModel != null)
                        onMiddleReleaseOnModel.Invoke();
                    break;
            }
        }

        #endregion
    }
}
